package belf.migrate.api.queue;

import belf.migrate.api.util.EngineConf;
import org.redisson.Redisson;
import org.redisson.api.RQueue;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

/**
 * 使用jedis作为redis客户端。全局共享唯一实例，在引擎退出时需要自动关闭
 */
public class RedisClient implements AutoCloseable {

    private static final RedisClient instance = new RedisClient();

    private final RedissonClient redisson;

    private RedisClient() {
        String redisHost = EngineConf.getInstance().getString("redis.host", "localhost");
        int redisPort = EngineConf.getInstance().getInt("redis.port", 6379);

        Config config = new Config();
        config.useSingleServer().setAddress(String.format("redis://%s:%d", redisHost,redisPort));

        redisson = Redisson.create(config);

    }

    public static RedisClient getInstance() {
        return instance;
    }

    @Override
    public void close() {
        redisson.shutdown();
    }

    public RedissonClient getConnection() {
        return redisson;
    }
}
